package cn.gailvlun.gll.presentation.login.viewmodel

import cn.gailvlun.gll.databinding.ActivityRegisterBinding
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.login.SendRegisterCaptchaReq
import cn.gailvlun.gll.net.login.SendRegisterCaptchaRes
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.base.WebViewActivity
import cn.gailvlun.gll.presentation.login.CaptchaActivity
import cn.gailvlun.gll.presentation.login.CountDownTimerWrapper
import cn.gailvlun.gll.presentation.login.FromActivityEnum
import cn.gailvlun.gll.presentation.login.LoginActivity
import cn.gailvlun.gll.presentation.login.adapter.LoginType
import cn.gailvlun.gll.util.*
import com.afollestad.materialdialogs.MaterialDialog
import com.blankj.utilcode.util.SPUtils



class RegisterViewModel(val activity: BaseActivity, private val mBinding: ActivityRegisterBinding) : BaseViewModel(activity) {

    fun onNext() {
        val phoneNumber = mBinding.cetPhone.text.toString().trim()
        val password = mBinding.petPassword.text.toString().trim()
        if (IsEmpty.string(phoneNumber)) {
            ToastUtil.showShort("请填写手机号")
            return
        }





        if (IsEmpty.string(password)) {
            ToastUtil.showShort("请填写密码")
            return
        }

        if (password.length < 6) {
            ToastUtil.showShort("密码不能小于6位")
            return
        }
        if (password.length > 16) {
            ToastUtil.showShort("密码不能多于16位")
            return
        }
        val cc = countryNameAndCode.split(" ")[1]
        val phoneNumberWithCc = "$cc$phoneNumber"

        if (CountDownTimerWrapper.phoneNumberWithCc == null || !CountDownTimerWrapper.phoneNumberWithCc.equals(phoneNumberWithCc)) {

            
            CountDownTimerWrapper.phoneNumberWithCc = phoneNumberWithCc
            CountDownTimerWrapper.countDown = 60L
            CountDownTimerWrapper.cancel()
            sendCode(phoneNumberWithCc, password)

        } else {
            if (CountDownTimerWrapper.countDown == 60L) {
                sendCode(phoneNumberWithCc, password)
            } else {
                CaptchaActivity.openActivity(activity, FromActivityEnum.RegisterActivity, CountDownTimerWrapper.phoneNumberWithCc!!, CountDownTimerWrapper.id!!, password)
            }
        }
    }

    private fun sendCode(phoneNumberWithCc: String, password: String) {
        val sendCode = SendRegisterCaptchaReq()
        sendCode.mobile = phoneNumberWithCc
        val pd = DialogUtil.showIndeterminate(activity)
        HttpMethods.getLoginService()
                .sendRegisterCaptcha(sendCode)
                .compose(RxUtil.applyScheduler())
                .subscribe(object : HttpSubscriber<SendRegisterCaptchaRes>() {
                    override fun onSuccess(response: SendRegisterCaptchaRes?) {
                        pd.dismiss()
                        CountDownTimerWrapper.phoneNumberWithCc = response!!.mobile
                        CountDownTimerWrapper.id = response.id
                        CountDownTimerWrapper.startOnlyOnce()
                        SPUtils.getInstance().put("loginType", LoginType.phone.name)
                        ToastUtil.showShort("验证码已发送，请查收短信")
                        CaptchaActivity.openActivity(activity, FromActivityEnum.RegisterActivity, response.mobile, response.id, password)
                    }

                    override fun onFailure(errMsg: String?, response: SendRegisterCaptchaRes?, code: Int) {
                        pd.dismiss()
                        if (code == 409) {
                            MaterialDialog.Builder(activity)
                                    .title("$errMsg")
                                    .positiveText("去登录")
                                    .onPositive { dialog, _ ->
                                        dialog.dismiss()
                                        LoginActivity.openActivity(activity, mBinding.cetPhone.text.toString().trim())
                                        activity.finish()
                                    }
                                    .negativeText("取消")
                                    .show()
                        } else {
                            ToastUtil.showShort(errMsg)
                        }
                    }
                })

    }


    fun onClickUserProtocol() {
        WebViewActivity.openUserProtocolActivity(activity)
    }
}